package com.gurb.android;

import java.io.File;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class ScannerClient implements MediaScannerConnectionClient {

	private File castDir = new File("/sdcard/podcast/");
	private MediaScannerConnection scanner  = null; 
	
	ScannerClient(File dir){
		castDir = dir;
	}
	
	public void setScanner(MediaScannerConnection scanner){	
		this.scanner = scanner;
	}
	
	private boolean isOK(File file){
		return (file.isFile() 
		&& file.canRead()
		&& (file.getName().toLowerCase().endsWith("mp3") 
			|| file.getName().toLowerCase().endsWith("ogg")));
	}
	
	public void onMediaScannerConnected() {
		final File[] fileList = castDir.listFiles();
		File mpFile = null;
		for (final File file : fileList) {
			if (isOK(file)){
				mpFile = file;
				scanner.scanFile(mpFile.getAbsolutePath(), null);
				Log.d("scanner", mpFile.getAbsolutePath());
			}	
		}
	}

	public void onScanCompleted(String path, Uri uri) {
		Log.d("MPSCAN", "Complete"
				+ MediaStore.getMediaScannerUri());
		scanner.disconnect();
	}

}
